package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> handlerSqlException1(SQLIntegrityConstraintViolationException ex){
        //获取异常信息
        String message = ex.getMessage();
        //解析出用户名
        String username = message.split(" ")[2];
        //响应错误信息
        return R.error(username+"已存在，请勿重复添加");
    }


    @ExceptionHandler(CustomerException.class)
    public R<String> handlerSqlException2(CustomerException ex){
        log.info(ex.getMessage());
        //返回异常信息提示
        return R.error(ex.getMessage());
    }

}
